package article.service.impl;


import article.dao.ArticleDao;
import article.entity.ArticleVO;
import article.service.ArticleService;
import articleLabel.entity.ArticleLabelVO;
import blogData.dao.BlogDataDao;
import blogData.entity.BlogDataVO;
import category.dao.CategoryDao;
import category.entity.CategoryVO;
import generic.page.Pages;
import label.dao.LabelDao;
import label.entity.LabelVO;
import util.ObtainOther;

import java.util.List;

public class ArticleServiceImpl implements ArticleService {

    private CategoryVO categoryVOE;

    private LabelVO labelVOE;

    private CategoryDao categoryDao;

    private ArticleDao articleDao;

    private LabelDao labelDao;

    private BlogDataDao blogDataDao;

    public void setBlogDataDao(BlogDataDao blogDataDao) {
        this.blogDataDao = blogDataDao;
    }

    public void setLabelDao(LabelDao labelDao) {
        this.labelDao = labelDao;
    }

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    public void setArticleDao(ArticleDao articleDao) {
        this.articleDao = articleDao;
    }

    @Override
    public ArticleVO findArticleVOById(Integer articleId) {

        return this.articleDao.findArticleVOById(articleId);
    }

    @Override
    public void deleteArticleById(Integer articleId) {
        this.articleDao.deleteArticleById(articleId);
    }

    @Override
    public ArticleVO updateArticle(ArticleVO articleVO) {
        return this.articleDao.updateArticle(articleVO);
    }

    @Override
    public ArticleVO addArticleVO(ArticleVO articleVO, BlogDataVO blogDataVO, CategoryVO categoryVO) {

        blogDataVO.getArticleVOs().add(articleVO);
        categoryVO.getArticleVOs().add(articleVO);

        articleVO.setBlogDataVO(blogDataVO);
        articleVO.setCategoryVO(categoryVO);

        return this.articleDao.addArticleVO(articleVO);
    }

    @Override
    public Pages queryPageByCategory(Pages pages, Integer categoryId) {
        if (pages == null) {
            pages = new Pages();
        }
        return this.articleDao.queryPageByCategory(pages, categoryId);
    }

    @Override
    public Pages queryPageByBlogData(Pages pages, Integer blogDataId) {
        if (pages == null) {
            pages = new Pages();
        }
        return this.articleDao.queryPageByBlogData(pages, blogDataId);
    }

    @Override
    public ArticleVO addArticleVOAll(ArticleVO articleVO, BlogDataVO blogDataVO, CategoryVO categoryVO, List<LabelVO> labelVOs) {
        labelVOs= ObtainOther.obtainLabels(labelVOs);
        for (int i = 0; i < labelVOs.size(); i++) {
            LabelVO labelVO = labelVOs.get(i);
            ArticleLabelVO al = new ArticleLabelVO(articleVO, labelVO);
            labelVO.getArticleLabelVOs().add(al);
            articleVO.getArticleLabelVOs().add(al);
        }
        blogDataVO.getArticleVOs().add(articleVO);
        categoryVO.getArticleVOs().add(articleVO);
        articleVO.setBlogDataVO(blogDataVO);
        articleVO.setCategoryVO(categoryVO);
        return this.articleDao.addArticleVO(articleVO);
    }
}
